'use strict';

/**
 * @ngdoc directive
 * @name myAppApp.directive:backBtn
 * @description
 * # backBtn
 */
angular.module('jgappApp')
    .directive('backBtn', function(Util) {
        return {
            scope: {
                fn: "=fn"
            },
            template: '<div class="backbtn">返回</div>',
            restrict: 'AE',
            replace: true,
            link: function postLink(scope, element, attrs) {
                // element.text('this is the backBtn directive');
                element.bind('click', function() {
                    if (attrs.cleancache != "no") {
                        Util.clearFormData();
                    }
                    var num = Util.getRelMoNum()
                    if (num) {
                        Util.setRelMoNum(0)
                    } else {
                        Util.clearTVType()
                    }
                    if (scope.fn) {
                        scope.fn();
                    } else {
                        Util.goBack();
                    }
                });
            }
        };
    }).directive("ngTouchstart", function() {
        return {
            controller: ["$scope", "$element", function($scope, $element) {

                $element.bind("touchstart", onTouchStart);

                function onTouchStart(event) {
                    var method = $element.attr("ng-touchstart");
                    $scope.$event = event;
                    $scope.$apply(method);
                }

            }]
        }
    })
    .directive("ngTouchmove", function() {
        return {
            controller: ["$scope", "$element", function($scope, $element) {

                $element.bind("touchstart", onTouchStart);

                function onTouchStart(event) {
                    event.preventDefault();
                    $element.bind("touchmove", onTouchMove);
                    $element.bind("touchend", onTouchEnd);
                }

                function onTouchMove(event) {
                    var method = $element.attr("ng-touchmove");
                    $scope.$event = event;
                    $scope.$apply(method);
                }

                function onTouchEnd(event) {
                    event.preventDefault();
                    $element.unbind("touchmove", onTouchMove);
                    $element.unbind("touchend", onTouchEnd);
                }

            }]
        }
    })
    .directive("ngTouchend", function() {
        return {
            controller: ["$scope", "$element", function($scope, $element) {

                $element.bind("touchend", onTouchEnd);

                function onTouchEnd(event) {
                    var method = $element.attr("ng-touchend");
                    $scope.$event = event;
                    $scope.$apply(method);
                }

            }]
        }
    });
